import { useApp } from "@inbiz/utils";
import { getMessage, getMessageGroup } from "../service";

export const useGetMessageEffect = () => {
  const requestServer = async ({
    group,
    pageIndex = 1,
    allGroup,
    callback,
    pageNumber,
    readState,
  }: {
    group?: any;
    pageIndex?: number;
    pageNumber?: number;
    allGroup: any;
    callback?: Function;
    readState: number;
  }) => {
    let res = await getMessage({
      pageIndex: pageIndex,
      pageSize: pageNumber,
      readState: readState,
      group: allGroup
        ? allGroup
        : [
            {
              groupKey: group.GroupKey,
              appId: group.AppId,
            },
          ],
    });
    if (res) {
      callback && callback(res);
    }
  };
  return requestServer;
};

// 获取有消息得分组
export const useGetMessageGroupEffect = () => {
  const appInfo = useApp();
  const requestServer = async (group: any, callback: Function) => {
    let res = await getMessageGroup({
      siteOutId: appInfo?.SiteInfo?.OutId,
      dataVersionId: appInfo?.SiteInfo?.DataVersionId,
      appId: appInfo?.AppId,
      group: group,
    });
    if (!res) {
      // inbizMessage('消息获取失败');
      return;
    }
    if (callback) callback(res);
  };

  return requestServer;
};